package cn.zhaotianen.codegenerator.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;

/**
 * <p/>
 * <pre>
 * ***************************************************************
 *  Copyright (c) 2014-2015 –苏州犀牛网络科技有限公司
 *  Package: PACKAGE_NAME
 *  Description:
 *  @since 1.0.0
 *  @author 赵天恩
 *  @date 2017/2/20
 *  @time 13:25
 * ***************************************************************
 * </pre>
 */
public class XmlToJson {

    /**
     * xml转json
     * @param xmlFilePath
     * @return
     */
    public static String xml2Json(String xmlFilePath){
        //读取resourse下的xml转成json
        File file=new File(xmlFilePath);
        String jsonString="";
        try {
            BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            String string=null;
            StringBuffer sb = new StringBuffer();
            while((string = reader.readLine())!=null){
                sb.append(string);
            }
            JSONObject jsonObject=cn.zhaotianen.codegenerator.tools.XmlTool.documentToJSONObject(sb.toString());
            jsonString=JSON.toJSONString(jsonObject);
            //这一句的输出,也许你很快的就知道原理了,其实原理很简单的！
            System.out.println("jsonString:"+jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    public static void main(String[] args) throws IOException {
        String xmlFilePath=XmlToJson.class.getResource("DataType.xml").getFile();
        xml2Json(xmlFilePath);
    }
}
